package com.zgm.ch05.aop.throwsAdvice._3_5_6;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class SimpleThrowsAdvice implements ThrowsAdvice {

	
	public static void main(String[] args) {
		ErrorBean target = new ErrorBean();
		ProxyFactory pf = new ProxyFactory();
		
		pf.addAdvice(new SimpleThrowsAdvice());
		pf.setTarget(target);
		
		ErrorBean proxy = (ErrorBean) pf.getProxy();
		
		try {
			proxy.errorProneMethod();
		} catch (Exception e) {
//			e.printStackTrace();
		}
		
		try {
			proxy.otherErrorProneMethod();
		} catch (Exception e) {
//			e.printStackTrace();
		}
	}
	
	
	public void afterThrowing(Exception ex) throws Throwable {
		System.out.println("****");
		System.out.println("捕获原始异常：");
		System.out.println(ex.getClass().getName());
		System.out.println("****\n");
	}
	
	public void afterThrowing(Method method, Object[] args, Object target, IllegalArgumentException ex) throws Throwable {
		System.out.println("****");
		System.out.println("捕获非法参数异常：");
		System.out.println(ex.getClass().getName());
		System.out.println("****\n");
	}
}
